package cz.czu.pef.DataModelling;

import java.util.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.*;

import org.hibernate.annotations.Index;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

@Entity
@Table(name="MACHINE_TYPES")
@Inheritance(strategy=InheritanceType.JOINED)
@ManagedBean
@ViewScoped
@Component
public class MachineType extends IdPersistenceObject
{
	private static final long serialVersionUID = 1794777862769026502L;

	@Column(name="MACHINE_TYPE_NAME", length=200, nullable=false)
	@Index(name="IDX_MACHINE_TYPES_NAME")
	private String machineTypeName;

	@Column(name="MACHINE_TYPE_DESCRIPTION", length=1024)
	private String machineTypeDescription;

	@OneToMany(fetch=FetchType.LAZY, targetEntity=Machinery.class, mappedBy="machineType")
	private Set<Machinery> machinery;

	public MachineType(String machineTypeName, String machineTypeDescription)
	{
		super();
		this.machineTypeName = machineTypeName;
		this.machineTypeDescription = machineTypeDescription;
	}
	public MachineType()
	{
	}
	
	public String getMachineTypeName()
	{
		return machineTypeName;
	}
	
	public void setMachineTypeName(String machineTypeName)
	{
		this.machineTypeName = machineTypeName;
	}
	
	public String getMachineTypeDescription()
	{
		return machineTypeDescription;
	}
	
	public void setMachineTypeDescription(String machineTypeDescription)
	{
		this.machineTypeDescription = machineTypeDescription;
	}
	
	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Set<Machinery> getMachinery()
	{
		return machinery;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("MachineType [machineTypeName=");
		builder.append(machineTypeName);
		builder.append(", machineTypeDescription=");
		builder.append(machineTypeDescription);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/**
	 * Can be deleted after deleting all referenced machineries.
	 */
	@Override
	public boolean canBeRemoved()
	{
		return (getMachinery().size() == 0);
	}
}
